# -*- coding: utf-8 -*-

import requests
from lxml import etree
import re
from tools import log_tool
from tools.auxiliary_tools import normalization


class Geturl:

    def __init__(self):
        """
        初始化参数
        """
        self.headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
        }
        self.log = log_tool.Logger(r'D:\PyCharm Community Edition 2020.3.3\files\NBA\test\log\get_url_info.log',
                                   level='debug')

    def get_playerlist_link(self):
        """
        获取教练与球员的链接
        :return:
        """
        url = 'http://www.stat-nba.com/playerList.php'

        # 传递参数的列表
        search_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
                       'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

        # 存放教练链接的列表
        coach_list = []
        # 存放球员链接的列表
        player_list = []
        # 存放球员常规赛链接的列表
        player_season_list = []
        # 存放球员季后赛链接的列表
        player_playoff_list = []
        # 存放球员全名星链接的列表
        player_allstar_list = []
        # 存放教练姓名的字典
        coach_name_dic = {}
        # 存放球员姓名的字典
        player_name_dic = {}

        for item in search_list:
            params = {
                'il': item,
                'lil': '0'
            }

            # 获取HTML
            res = requests.get(url, headers=self.headers, params=params)
            res.encoding = 'utf-8'
            html = etree.HTML(res.text)

            # 获取列表标题个数，用来判断HTML中是否存在包含教练链接的列表
            content_one = html.xpath('//div[@class="title"]')

            if len(content_one) == 2:
                content_two = html.xpath('//div[@class="playerList"]')

                # 教练链接
                c_list = content_two[0].xpath('.//a')
                for i in c_list:
                    url_one = i.xpath('./@href')[0][1:]
                    name_one = normalization(i.xpath('./span/text()'))[0]
                    num_one = re.findall(r'\d+', url_one)
                    coach_list.append('http://www.stat-nba.com' + url_one)  # 获得拼接教练的球员URL
                    coach_name_dic[num_one[0]] = name_one  # 获得教练数字对应名字字典

                # 球员链接
                p_list = content_two[1].xpath('.//a')
                for i in p_list:
                    url_two = i.xpath('./@href')[0][1:]
                    name_two = normalization(i.xpath('./span/text()'))[0]
                    num_two = re.findall(r'\d+', url_two)
                    player_list.append('http://www.stat-nba.com' + url_two)  # 获得拼接完整的球员URL

                    str_one = 'http://www.stat-nba.com/player/stat_box/{}_season.html'.format(num_two[0])
                    player_season_list.append(str_one)  # 获得拼接完整的常规赛URL

                    str_two = 'http://www.stat-nba.com/player/stat_box/{}_playoff.html'.format(num_two[0])
                    player_playoff_list.append(str_two)  # 获得拼接完整的季后赛URL

                    str_three = 'http://www.stat-nba.com/player/stat_box/{}_allstar.html'.format(num_two[0])
                    player_allstar_list.append(str_three)  # 获得拼接完整的全明星URL

                    player_name_dic[num_two[0]] = name_two  # 获得球员数字对应名字字典

            elif len(content_one) == 1:
                content_two = html.xpath('//div[@class="playerList"]')
                p_list = content_two[0].xpath('.//a')
                for i in p_list:
                    url_two = i.xpath('./@href')[0][1:]
                    name_two = normalization(i.xpath('./span/text()'))[0]
                    num_two = re.findall(r'\d+', url_two)
                    player_list.append('http://www.stat-nba.com' + url_two)  # 获得拼接完整的球员URL

                    str_one = 'http://www.stat-nba.com/player/stat_box/{}_season.html'.format(num_two[0])
                    player_season_list.append(str_one)  # 获得拼接完整的常规赛URL

                    str_two = 'http://www.stat-nba.com/player/stat_box/{}_playoff.html'.format(num_two[0])
                    player_playoff_list.append(str_two)  # 获得拼接完整的季后赛URL

                    str_three = 'http://www.stat-nba.com/player/stat_box/{}_allstar.html'.format(num_two[0])
                    player_allstar_list.append(str_three)  # 获得拼接完整的全明星URL

                    player_name_dic[num_two[0]] = name_two  # 获得球员数字对应名字字典

        return coach_list, player_list, player_season_list, player_playoff_list, \
            player_allstar_list, coach_name_dic, player_name_dic


if __name__ == '__main__':
    test_one = Geturl()

    print(test_one.get_playerlist_link()[1])

